{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Counting Elements"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #hash-table"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #哈希表"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: countElements"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #数元素"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个整数数组&nbsp;<code>arr</code>， 对于元素 <code>x</code> ，只有当 <code>x + 1</code> 也在数组&nbsp;<code>arr</code> 里时，才能记为 <code>1</code> 个数。</p>\n",
    "\n",
    "<p>如果数组&nbsp;<code>arr</code> 里有重复的数，每个重复的数单独计算。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>arr = [1,2,3]\n",
    "<strong>输出：</strong>2\n",
    "<strong>解释：</strong>1 和 2 被计算次数因为 2 和 3 在数组 arr 里。</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>arr = [1,1,3,3,5,5,7,7]\n",
    "<strong>输出：</strong>0\n",
    "<strong>解释：</strong>所有的数都不算, 因为数组里没有 2、4、6、8。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= arr.length &lt;= 1000</code></li>\n",
    "\t<li><code>0 &lt;= arr[i] &lt;= 1000</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [counting-elements](https://leetcode.cn/problems/counting-elements/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [counting-elements](https://leetcode.cn/problems/counting-elements/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[1,2,3]', '[1,1,3,3,5,5,7,7]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, arr: List[int]) -> int:\n",
    "        vis = set(arr)\n",
    "        ans = 0\n",
    "        for num in arr:\n",
    "            if num + 1 in vis:\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, arr: List[int]) -> int:\n",
    "        count=0\n",
    "        for num in arr:\n",
    "            if num+1 in arr:\n",
    "                count+=1\n",
    "        return count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "class Solution:\n",
    "    def countElements(self, arr: List[int]) -> int:\n",
    "        ans = 0\n",
    "        d = Counter(arr)\n",
    "        for num in d:\n",
    "            if d[num+1]:\n",
    "                ans += d[num]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, arr: List[int]) -> int:\n",
    "        c = collections.Counter(arr)\n",
    "      \n",
    "        cnt = 0\n",
    "        ck = list(c.keys())\n",
    "        ck.sort()\n",
    "       \n",
    "        for i in range(len(ck) - 1):\n",
    "            if ck[i] + 1 == ck[i+1]:\n",
    "                cnt += c[ck[i]]\n",
    "           \n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, arr: List[int]) -> int:\n",
    "        count = 0\n",
    "        for x in arr:\n",
    "            if x+1 in arr:\n",
    "                count+=1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, arr: List[int]) -> int:\n",
    "        ans = 0\n",
    "        dic = set()\n",
    "        for x in arr:\n",
    "            dic.add(x)\n",
    "        for x in arr:\n",
    "            if (x+1) in dic:\n",
    "                ans += 1\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, arr: List[int]) -> int:\n",
    "        return sum(arr[i] + 1 in arr for i in range(len(arr)))\n",
    "        # return sum(arr[i]+1 in set(arr)  for i in range(len(arr)))\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, arr: List[int]) -> int:\n",
    "        map_ = {}\n",
    "        for a in arr:\n",
    "            map_[a] = map_.get(a,0) + 1\n",
    "        \n",
    "        res = 0\n",
    "        for k, v in map_.items():\n",
    "            if k + 1 in map_:\n",
    "                res += map_[k]\n",
    "        \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, arr: List[int]) -> int:\n",
    "        return sum(arr[i]+1 in set(arr)  for i in range(len(arr)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, arr: List[int]) -> int:\n",
    "        numset = set(arr)\n",
    "        count = 0\n",
    "        for i in arr:\n",
    "            if i + 1 in numset:\n",
    "                count += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, arr: List[int]) -> int:\n",
    "        sarr = list(set(arr))\n",
    "        sarr.sort()\n",
    "        cnt = 0\n",
    "        for i in range(len(sarr) - 1):\n",
    "            if sarr[i] + 1 == sarr[i + 1]:\n",
    "                cnt += arr.count(sarr[i])\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, arr: List[int]) -> int:\n",
    "        c = collections.Counter(arr)\n",
    "      \n",
    "        cnt = 0\n",
    "        ck = list(c.keys())\n",
    "        ck.sort()\n",
    "       \n",
    "        for i in range(len(ck) - 1):\n",
    "            if ck[i] + 1 == ck[i+1]:\n",
    "                cnt += c[ck[i]]\n",
    "           \n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, arr: List[int]) -> int:\n",
    "        count = 0\n",
    "\n",
    "        for i in arr:\n",
    "            if i+1 in arr:\n",
    "                count += 1\n",
    "\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, arr: List[int]) -> int:\n",
    "        ans = 0\n",
    "        c = collections.Counter(arr)\n",
    "        for i in c:\n",
    "            if i+1 in c:\n",
    "                ans += c[i]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, arr: List[int]) -> int:\n",
    "        s = set(arr)\n",
    "        res = 0\n",
    "        for a in arr:\n",
    "            if a+1 in s: res+=1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, arr: List[int]) -> int:\n",
    "        D = dict()\n",
    "        for i in arr:\n",
    "            if i in D.keys():\n",
    "                D[i] += 1\n",
    "            else:\n",
    "                D[i] = 1\n",
    "        result = 0\n",
    "        for key, value in D.items():\n",
    "            if key + 1 in D.keys():\n",
    "                result += value\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, arr: List[int]) -> int:\n",
    "        sarr = list(set(arr))\n",
    "        sarr.sort()\n",
    "        cnt = 0\n",
    "        for i in range(len(sarr) - 1):\n",
    "            if sarr[i] + 1 == sarr[i + 1]:\n",
    "                cnt += arr.count(sarr[i])\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, arr: List[int]) -> int:\n",
    "        res = 0\n",
    "        for x in arr:\n",
    "            if x+1 in arr:\n",
    "                res += 1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, arr: List[int]) -> int:\n",
    "\n",
    "        look_up = defaultdict(int)\n",
    "        for i in arr:\n",
    "            look_up[i] += 1\n",
    "        \n",
    "        cnt = 0\n",
    "        for i in arr:\n",
    "            if i+1 in look_up:\n",
    "                cnt += 1\n",
    "        \n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, arr: List[int]) -> int:\n",
    "        c = collections.Counter(arr)\n",
    "\n",
    "        ret = 0\n",
    "        for k, v in c.items():\n",
    "            if k + 1 in c:\n",
    "                ret += v\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, arr: List[int]) -> int:\n",
    "        count=0\n",
    "        for i in arr:\n",
    "            if i+1 in arr:\n",
    "                count+=1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, arr: List[int]) -> int:\n",
    "        hashmap = {}\n",
    "        count = 0\n",
    "        for i in arr:\n",
    "            if (i+1) in arr:\n",
    "                count+=1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, arr: List[int]) -> int:\n",
    "\n",
    "        if not arr:\n",
    "            return 0 \n",
    "        \n",
    "\n",
    "        arr_set = set(arr)\n",
    "        cnt = 0\n",
    "        for x in arr:\n",
    "            if x+1 in arr_set:\n",
    "                cnt += 1\n",
    "        \n",
    "        return cnt\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, arr: List[int]) -> int:\n",
    "        c = 0\n",
    "        for i in arr:\n",
    "            if i+1 in arr:\n",
    "                c +=1\n",
    "        return c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, arr: List[int]) -> int:\n",
    "        count = 0\n",
    "        arr.sort()\n",
    "        prev, curr = 0, 0\n",
    "        while curr < len(arr) and prev < len(arr):\n",
    "            while curr < len(arr) and arr[prev] == arr[curr]:\n",
    "                curr += 1\n",
    "\n",
    "            if curr < len(arr) and arr[prev] + 1 == arr[curr]:\n",
    "                count += 1\n",
    "\n",
    "            prev += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, arr: List[int]) -> int:\n",
    "        c = collections.Counter(arr)\n",
    "      \n",
    "        cnt = 0\n",
    "        ck = list(c.keys())\n",
    "        ck.sort()\n",
    "       \n",
    "        for i in range(len(ck) - 1):\n",
    "            if ck[i] + 1 == ck[i+1]:\n",
    "                cnt += c[ck[i]]\n",
    "           \n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, arr: List[int]) -> int:\n",
    "        cnt = Counter(arr)\n",
    "        ans = 0\n",
    "        for k, v in cnt.items():\n",
    "            if k + 1 in cnt:\n",
    "                ans += v\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, arr: List[int]) -> int:\n",
    "        check = set(arr)\n",
    "        res = 0\n",
    "        hashmap = {}\n",
    "        for i in arr:\n",
    "            hashmap[i] = hashmap.get(i, 0) + 1\n",
    "        for j in check:\n",
    "            if j+1 not in hashmap.keys():\n",
    "                continue\n",
    "            else:\n",
    "                res += hashmap[j]\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, arr: List[int]) -> int:\n",
    "        arr.sort(key = lambda x: -x)\n",
    "        dic = collections.defaultdict(int)\n",
    "        res = 0\n",
    "        for i in range(len(arr)):\n",
    "            dic[arr[i]] += 1\n",
    "            if dic[arr[i] + 1] > 0:\n",
    "                res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, arr: List[int]) -> int:\n",
    "        # unique_arr = set(arr)\n",
    "        # # 就算有重复也只能被计算一次\n",
    "        # sorted_arr = sorted(unique_arr,reverse=False)\n",
    "        # print(sorted_arr)\n",
    "\n",
    "        # count = 0\n",
    "        # for i in range(1,len(sorted_arr)):\n",
    "        #     if sorted_arr[i-1]+1 == sorted_arr[i]:\n",
    "        #         count+=1\n",
    "        # return count\n",
    "\n",
    "        return sum(arr[i]+1 in set(arr)  for i in range(len(arr)))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, arr: List[int]) -> int:\n",
    "        arr = Counter(arr)\n",
    "        res = 0\n",
    "\n",
    "        for i in arr:\n",
    "            if i + 1 in arr:\n",
    "                res += arr[i]\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, arr: List[int]) -> int:\n",
    "        s = set(arr)\n",
    "        ans = 0\n",
    "        for num in arr:\n",
    "            if num+1 in s:\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, arr: List[int]) -> int:\n",
    "        c = collections.Counter(arr)\n",
    "        cnt = 0\n",
    "        ck = list(c.keys())\n",
    "        ck.sort()\n",
    "       \n",
    "        for i in range(len(ck) - 1):\n",
    "            if ck[i] + 1 == ck[i+1]:\n",
    "                cnt += c[ck[i]]\n",
    "           \n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, arr: List[int]) -> int:\n",
    "        res = 0\n",
    "        cnt = Counter(arr)\n",
    "        for k, v in cnt.items():\n",
    "            if k + 1 in cnt:\n",
    "                res += v\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, arr: List[int]) -> int:\n",
    "        sum = 0\n",
    "        for num in arr:\n",
    "            if num + 1 in arr:\n",
    "              sum += 1\n",
    "\n",
    "        return sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, arr: List[int]) -> int:\n",
    "        count = 0\n",
    "        for i in range(len(arr)):\n",
    "            tmpval = arr[i] + 1\n",
    "            if tmpval in arr:\n",
    "                count += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, arr: List[int]) -> int:\n",
    "        t=set(arr)\n",
    "        ans=0\n",
    "        for i in arr:\n",
    "            if i+1 in t:\n",
    "                ans+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, arr: List[int]) -> int:\n",
    "        set_arr = set(arr)\n",
    "        count = 0\n",
    "        for i in arr:\n",
    "            if i + 1 in set_arr:\n",
    "                count += 1\n",
    "        return count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, arr: List[int]) -> int:\n",
    "        return sum(arr[i]+1 in set(arr)  for i in range(len(arr)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, arr: List[int]) -> int:\n",
    "        bucket = [0 for _ in range(1005)]\n",
    "        for n in arr:\n",
    "            bucket[n] += 1\n",
    "        counter = 0\n",
    "        for i in range(len(bucket)-1):\n",
    "            if bucket[i] and bucket[i+1]:\n",
    "                counter += bucket[i]\n",
    "        return counter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, arr: List[int]) -> int:\n",
    "        a = {}\n",
    "        for x in arr:\n",
    "            if x in a:\n",
    "                a[x] += 1\n",
    "            else:\n",
    "                a[x] = 1\n",
    "        r = 0\n",
    "        for x in a:\n",
    "            if x+1 in a:\n",
    "                r += a[x]\n",
    "        return r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import defaultdict\n",
    "class Solution:\n",
    "    def countElements(self, arr: List[int]) -> int:\n",
    "        dic = defaultdict(int)\n",
    "        count = 0\n",
    "        for i in range(len(arr)):\n",
    "            dic[arr[i]]+=1\n",
    "        for i in dic:\n",
    "            if i+1 in dic:\n",
    "                count += dic.get(i)\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, arr: List[int]) -> int:\n",
    "        sarr = list(set(arr))\n",
    "        sarr.sort()\n",
    "        cnt = 0\n",
    "        for i in range(len(sarr) - 1):\n",
    "            if sarr[i] + 1 == sarr[i + 1]:\n",
    "                cnt += arr.count(sarr[i])\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, arr: List[int]) -> int:\n",
    "        result = 0\n",
    "        for num in arr:\n",
    "            if num + 1 in arr:\n",
    "                result += 1\n",
    "\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, arr: List[int]) -> int:\n",
    "        c = collections.Counter(arr)\n",
    "        cnt = 0\n",
    "        ck = list(c.keys())\n",
    "        ck.sort()\n",
    "        print(ck)\n",
    "        for i in range(len(ck) - 1):\n",
    "            if ck[i] + 1 == ck[i+1]:\n",
    "                cnt += c[ck[i]]\n",
    "           \n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, arr: List[int]) -> int:\n",
    "        m = set(arr)\n",
    "        num = 0\n",
    "        for i in arr:\n",
    "            if i+1 in m:\n",
    "                num+=1\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# 哈希 + 计数\n",
    "class Solution:\n",
    "    def countElements(self, arr: List[int]) -> int:\n",
    "        # # 数组解法\n",
    "        # res = 0\n",
    "        # for a in arr:\n",
    "        #     if a + 1 in arr:\n",
    "        #         res += 1\n",
    "        # return res \n",
    "\n",
    "        # 哈希解法\n",
    "        h_dict = dict()\n",
    "        for a in arr:\n",
    "            h_dict[a] = h_dict.get(a, 0) + 1\n",
    "        res = 0\n",
    "        for k, v in h_dict.items():\n",
    "            if k + 1 in h_dict:\n",
    "                res += h_dict[k]\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, arr: List[int]) -> int:\n",
    "        count=0\n",
    "        for i in arr:\n",
    "            if i+1 in arr:\n",
    "                count+=1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, arr: List[int]) -> int:\n",
    "        t=Counter(arr)\n",
    "        res=0\n",
    "        for k,v in t.items():\n",
    "            if k+1 in t:\n",
    "                res+=v\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, arr: List[int]) -> int:\n",
    "        memo=set()\n",
    "        arr.sort(reverse=True)\n",
    "        \n",
    "        res=0\n",
    "        for v in arr:\n",
    "            if(v+1 in memo):\n",
    "                res+=1\n",
    "            memo.add(v)\n",
    "        \n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# 哈希 + 计数\n",
    "class Solution:\n",
    "    def countElements(self, arr: List[int]) -> int:\n",
    "        # # 数组解法\n",
    "        # res = 0\n",
    "        # for a in arr:\n",
    "        #     if a + 1 in arr:\n",
    "        #         res += 1\n",
    "        # return res \n",
    "\n",
    "        # 哈希解法（性能更优）\n",
    "        h_dict = dict()\n",
    "        for a in arr:\n",
    "            h_dict[a] = h_dict.get(a, 0) + 1\n",
    "        res = 0\n",
    "        for k, v in h_dict.items():\n",
    "            if k + 1 in h_dict:\n",
    "                res += h_dict[k]\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# 哈希 + 计数\n",
    "class Solution:\n",
    "    def countElements(self, arr: List[int]) -> int:\n",
    "        # 数组解法\n",
    "        res = 0\n",
    "        for a in arr:\n",
    "            if a + 1 in arr:\n",
    "                res += 1\n",
    "        return res \n",
    "\n",
    "        # 哈希解法\n",
    "        h_dict = dict()\n",
    "        for a in arr:\n",
    "            h_dict[a] = h_dict.get(a, 0) + 1\n",
    "        res = 0\n",
    "        for k, v in h_dict.items():\n",
    "            if k + 1 in h_dict:\n",
    "                res += h_dict[k]\n",
    "        return res\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
